import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class goSpace extends JFrame {
    private JLabel currentTimeLabel;
    private JLabel destinationTimeLabel;
    private JComboBox<String> timeTravelOptions;
    private JButton travelButton;
    private JProgressBar progressBar;
    private int progressValue;
    private Timer progressTimer; // 添加progressTimer声明

    public goSpace() {
        setTitle("时空穿越器");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        initComponents();

        setLayout(new GridLayout(6, 1, 10, 10));

        add(new JLabel("当前时间", SwingConstants.CENTER));
        add(currentTimeLabel);
        add(new JLabel("选择穿越时间:", SwingConstants.CENTER));
        add(timeTravelOptions);
        add(progressBar);
        add(travelButton);

        updateCurrentTime();

        new Timer(1000, e -> updateCurrentTime()).start();
    }

    private void initComponents() {
        currentTimeLabel = new JLabel("", SwingConstants.CENTER);
        currentTimeLabel.setFont(new Font("Arial", Font.BOLD, 18));

        destinationTimeLabel = new JLabel("", SwingConstants.CENTER);
        destinationTimeLabel.setFont(new Font("Arial", Font.BOLD, 18));

        String[] options = {
                "1秒后", "1分钟后", "1小时后", "1天后", "1周后", "1月后", "1年后",
                "1秒前", "1分钟前", "1小时前", "1天前", "1周前", "1月前", "1年前"
        };
        timeTravelOptions = new JComboBox<>(options);

        progressBar = new JProgressBar(0, 100);
        progressBar.setStringPainted(true);
        progressBar.setString("准备穿越...");
        progressBar.setForeground(new Color(0, 150, 255));
        progressBar.setVisible(false);

        travelButton = new JButton("开始穿越！");
        travelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                startTimeTravel(); // 修正方法名调用
            }
        });
    }

    private void updateCurrentTime() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        currentTimeLabel.setText(now.format(formatter));
    }

    private void startTimeTravel() { // 修正方法名
        travelButton.setEnabled(false);
        timeTravelOptions.setEnabled(false);
        progressBar.setVisible(true);
        progressValue = 0;

        progressTimer = new Timer(50, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                progressValue += 1;
                progressBar.setValue(progressValue);
                progressBar.setString("穿越中..." + progressValue + "%");

                if (progressValue > 30 && progressValue < 70) {
                    progressBar.setString("穿越时空裂缝..." + progressValue + "%");
                } else if (progressValue > 70) {
                    progressBar.setString("即将到达..." + progressValue + "%");
                }
                if (progressValue >= 100) {
                    progressTimer.stop();
                    completeTimeTravel();
                }
            }
        });
        progressTimer.start();
    }

    private void completeTimeTravel() {
        String selectedOption = (String) timeTravelOptions.getSelectedItem();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime destinationTime = now;

        if (selectedOption != null) {
            String[] parts = selectedOption.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
            if (parts.length >= 2) {
                try {
                    int amount = Integer.parseInt(parts[0]); // 修正变量名拼写
                    String unit = parts[1];

                    switch (unit) {
                        case "秒后":
                            destinationTime = now.plusSeconds(amount);
                            break;
                        case "分钟后":
                            destinationTime = now.plusMinutes(amount);
                            break;
                        case "小时后":
                            destinationTime = now.plusHours(amount);
                            break;
                        case "天后":
                            destinationTime = now.plusDays(amount);
                            break;
                        case "周后":
                            destinationTime = now.plusWeeks(amount);
                            break;
                        case "月后":
                            destinationTime = now.plusMonths(amount);
                            break;
                        case "年后":
                            destinationTime = now.plusYears(amount);
                            break;
                        case "秒前":
                            destinationTime = now.minusSeconds(amount);
                            break;
                        case "分钟前":
                            destinationTime = now.minusMinutes(amount);
                            break;
                        case "小时前":
                            destinationTime = now.minusHours(amount);
                            break;
                        case "天前":
                            destinationTime = now.minusDays(amount);
                            break;
                        case "周前":
                            destinationTime = now.minusWeeks(amount);
                            break;
                        case "月前":
                            destinationTime = now.minusMonths(amount);
                            break;
                        case "年前":
                            destinationTime = now.minusYears(amount);
                            break;
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        }

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        progressBar.setString("穿越成功！");

        LocalDateTime finalDestinationTime = destinationTime;
        SwingUtilities.invokeLater(() -> {
            JOptionPane.showMessageDialog(this,
                    "穿越成功！\n目的地时间:" + finalDestinationTime.format(formatter),
                    "穿越结果", JOptionPane.INFORMATION_MESSAGE);

            progressBar.setVisible(false);
            travelButton.setEnabled(true);
            timeTravelOptions.setEnabled(true);
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            goSpace machine = new goSpace();
            machine.setVisible(true);
        });
    }
}